#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "../source/Buffer.hpp"

#include "http.hpp"

size_t Split(const std::string& src, const std::string& sep, std::vector<std::string>* const array)
{
    size_t pos = 0, offset = 0;
    while (offset < src.size())
    {
        pos = src.find(sep, offset);
        if (pos > offset)
            array->emplace_back(src.substr(offset, pos - offset));
        if (pos == std::string::npos) break; // 说明已经找完了
        offset = pos + sep.size();
    }
    return array->size();
}

static bool ReadFile(const std::string& filename, std::string* content)
{
    std::ifstream ifs(filename, std::ios::binary);
    if (ifs.is_open() == false)
    {
        printf("%s open failed!\n", filename.c_str());
        return false;
    }
    size_t fsize = 0;
    ifs.seekg(0, ifs.end);
    fsize = ifs.tellg();
    ifs.seekg(0, ifs.beg);
    content->resize(fsize);
    ifs.read(&(*content)[0], fsize);
    if (ifs.good() == false)
    {
        printf("%s read failed!\n", filename.c_str());
        ifs.close();
        return false;
    }
    ifs.close();
    return true;
}

static bool WriteFile(const std::string& filename, const std::string& content)
{
    std::ofstream ofs(filename, std::ios::binary | std::ios::trunc);
    if (ofs.is_open() == false)
    {
        printf("%s open failed!\n", filename.c_str());
        return false;
    }
    ofs.write(content.c_str(), content.size());
    if (ofs.good() == false)
    {
        printf("%s write failed!\n", filename.c_str());
        ofs.close();
        return false;
    }
    ofs.close();
    return true;
}

int main()
{
    std::string line = "GET / HTTP/1.1\r\n";
    std::smatch matches;
    std::regex e("(GET|POST|HEAD|PUT|DELETE) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?", std::regex::icase);
    bool ret = std::regex_match(line, matches, e);
    std::cout << ret << std::endl;
    for (const auto& str : matches)
    {
        std::cout << "[" << str << "]";
    }

    std::cout << Util::IsValidPath("/http/../kkk/../..") << std::endl;

    std::cout << Util::IsDirectory("../http") << std::endl;
    std::cout << Util::IsRegular("test.cc") << std::endl;

    std::string url = "/login?user:zs&password=666C  +";
    // std::string res = Util::UrlEncode(url, false);
    std::string res = Util::UrlEncode(url, true);
    std::cout << res << std::endl;
    std::string tmp = Util::UrlDecode(res, true);
    std::cout << tmp << std::endl;
    std::string statu_msg = Util::GetStatusDescribe(404);
    std::cout << statu_msg << std::endl;
    std::string mime_msg = Util::GetMime("a.tar.gz");
    std::cout << mime_msg << std::endl;
    

    // std::string content;
    // bool ret = ReadFile("http.hpp", &content);
    // if (ret == false) return 1;
    // WriteFile("test.hpp", content);

    // std::string str = "abc,de,,,fg,";
    // std::string str = "abc";
    // std::string str = ",,,abc";
    // std::vector<std::string> array;
    // Split(str, ",", &array);
    // for (auto& str : array)
    // {
    //     std::cout <<"[" << str << "]" << std::endl;
    // }

    return 0;
}